package org.ido.client.components;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author greenido
 */
public class SimpleDialog extends DialogBox implements KeyboardListener {
    // The answer is the index of the clicked button.
    private int answer;    // Optional textbox input
    private TextBox textBox;
    private VerticalPanel vp;
    private DialogListener listener;

    /**
     * 
     * @param title
     * @param description
     * @param buttonLabels
     * @param iconType - could be warning, error @see Utils.java
     */
    public SimpleDialog(String title, String description, String[] buttonLabels) {
        super(false, true);
        setText(title);
        vp = new VerticalPanel();
        
        Label l = new Label(description, true);
        HorizontalPanel imgText = new HorizontalPanel();
        imgText.setStyleName("text-vertical-middle-align");
        imgText.add(new HTML("<img src='etc/warning.png' alt='warning'>"));
        imgText.add(l);
        vp.add(imgText);

        HorizontalPanel p = new HorizontalPanel();
        p.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
        for (int i = 0; i < buttonLabels.length; i++) {
            Button b = new Button(buttonLabels[i]);
            b.addStyleName("edit-panel-base-button");
            p.add(b);
            final int index = i;
            b.addClickListener(new ClickListener() {

                public void onClick(Widget sender) {
                    answer = index;
                    finishUp();
                }
            });
        }
        vp.add(p);
        vp.addStyleName("simple-dialog");
        setWidget(vp);
    }


    public void setDialogListener(DialogListener listener) {
        this.listener = listener;
    }

    public void addTextBoxEntry() {
        textBox = new TextBox();
        textBox.addKeyboardListener(this);
        textBox.setWidth("80%");
        vp.insert(textBox, 1);
    }

    public void addWidget(Widget w) {
        vp.insert(w, 1);
    }

    public void show() {
        super.show();
        if (textBox != null) {
            textBox.setFocus(true);
        }
    }

    public void showAt(Widget w) {
        int x = w.getAbsoluteLeft();
        int y = w.getAbsoluteTop();
        setPopupPosition(x, y);
        show();
    }

    public String getTextEntry() {
        return textBox == null ? null : textBox.getText();
    }

    public int getAnswer() {
        return answer;
    }

    public void onKeyDown(Widget w, char keyCode, int modifiers) {
    }

    public void onKeyPress(Widget w, char keyCode, int modifiers) {
    }

    public void onKeyUp(Widget w, char keyCode, int modifiers) {
        if (keyCode == KEY_ENTER && modifiers == 0 && w instanceof TextBox) {
            answer = 0;
            finishUp();
        }
    }

    private void finishUp() {
        hide();
        if (listener != null) {
            listener.dialogFinished(this);
        }
    }
}